define(['jquery','bootstrap', 'backend', 'table','form','template'], function ($, undefined, Backend, Table, Form,Template) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'serial/serials/index',
                    add_url: 'serial/serials/add',
                    edit_url: 'serial/serials/edit',
                    del_url: 'serial/serials/del',
                    send_url: 'serial/serials/send',
                    fenleis_url: 'serial/serials/fenleis',
                    tovoid_url: 'serial/serials/tovoid',
                    returngoods_url: 'serial/serials/returngoods',
                    delivergoods_url: 'serial/serials/delivergoods',
                    kefusend_url: 'serial/serials/kefusend',
                    multi_url: 'serial/serials/multi',
                    table: 'serial',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'code', title: __('Code')},
                        {field: 'order_sn', title: __('订单号')},
                        {field: 'type', title: __('type'),formatter: Table.api.formatter.label,
                          searchList: function (column) {
                            return Template('categorytpl', {});
                          }},
                        {field: 'uid', title: __('Uid'),visible:false},
                        {field: 'kaoshi', title: __('Kaoshi'),visible:false},
                        {field: 'fahuo', title: __('Fahuo'),visible:false},
                        {field: 'send', title: __('Send'), searchList: {"1":__('Yes'),"0":__('No')}, formatter: Controller.api.formatter.sends},
                        {field: 'is_register', title: __('Is_register'), searchList: {"1":__('Yes'),"0":__('No')},formatter: Controller.api.formatter.register},
                        {field: 'is_cancel', title: __('Is_cancel'), searchList: {"1":__('Yes'),"0":__('No')},formatter: Controller.api.formatter.cancel},
                        {field: 'dealer_uid', title: __('Dealer_uid'), formatter: Controller.api.formatter.dealer,
                            searchList: function (column) {
                                return Template('dailishang', {});
                            }
                            },
                        {field: 'branch_uid', title: __('分公司'), formatter: Controller.api.formatter.branch,
                            searchList: function (column) {
                                return Template('fengongsi', {});
                            }
                        },
                        {field: 'action_time', title: __('激活时间'), visible:false,operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'sendtime', title: __('Sendtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);

          $(document).on("click", ".btn-default", function () {
            var form = $("form", table.$commonsearch);
            Form.events.cxselect(form);//初始化插件
            $("input[name='type']").val('');
          });
          // 监听下拉列表改变的事件
          $(document).on('change', '.two', function () {
              var obj =$(".two");
              var temp=[];
              for(var i=0;i<obj.length;i++){
                 if($(obj[i]).find('option:selected').val()!=undefined){
                     temp.push($(obj[i]).find('option:selected').val());
                 }

              }
              if(temp){
                  temp=temp.join();
              }else{
                  temp='';
              }
              if(temp!=''){
                  $("input[name='type']").val(temp);
              }
            });
            // 监听下拉列表改变的事件
            $(document).on('change', '#c-agent_name', function () {
                console.log($(this).val());
                $("input[name='dealer_uid']").val($(this).val());
            });

            // 监听下拉列表改变的事件
            $(document).on('change', '#c-agent_names', function () {
                console.log($(this).val());
                $("input[name='branch_uid']").val($(this).val());
            });
        },
        add: function () {
          $('#action').change(function () {
            if($('#action').val() == 2)
            {
              $('#userid').css('display','block');
              return false;
            }
            $('#userid').css('display','none');
            return false;
          });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        send: function () {
            Controller.api.bindevent();
        },
        fenleis: function () {
            Controller.api.bindevent();
        },
        tovoid: function () {
            Controller.api.bindevent();
        },
        returngoods: function () {
            Controller.api.bindevent();
        },
        delivergoods: function () {
            $('#action').change(function () {
              $('#fengongsi').remove();
              $('#dailishang').remove();
                if($('#action').val() == 2)
                {
                  var html = '<div class="form-group" id="dailishang">\n' +
                    '                        <label for="c-agent_name" class="control-label col-xs-12 col-sm-3">*代理商名称:</label>\n' +
                    '                        <div class="col-xs-12 col-sm-8">\n' +
                    '                            <input style="width:280px;" id="c-agent_name"  name="row[nameAgent]"  data-rule="required"  data-source="/api/base/getAgent" data-primary-key="agent_name" data-field="agent_name"   class="form-control selectpage"  type="text" value="">\n' +
                    '                        </div>\n' +
                    '                    </div>';
                    $("#dalishangs").append(html);
                  Form.events.selectpage("#send-form");
                  return false;
                }
                if($('#action').val() == 1)
                {
                  var html = "<div class=\"form-group\" id=\"fengongsi\" >\n" +
                    "                            <label for=\"c-fengongsi\" class=\"control-label col-xs-12 col-sm-3\">*分公司名称</label>\n" +
                    "                            <div class=\"col-xs-12 col-sm-8\">\n" +
                    "                                <input style=\"width:280px;\" id=\"c-fengongsi\"  name=\"row[githouse]\" data-rule=\"required\"  data-source=\"/api/base/githouse\" data-primary-key=\"gname\" data-field=\"gname\"   class=\"form-control selectpage\"  type=\"text\" value=\"\">\n" +
                    "                            </div>\n" +
                    "                        </div>";
                  $("#dalishangs").append(html);
                  Form.events.selectpage("#send-form");
                }

                return false;
            });
            Controller.api.bindevent();
        },
        kefusend: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
              Form.api.bindevent($("form[role=form]"),function (data,ret) {
                if(ret.url)
                {
                  window.open(ret.url);
                }
              }, function(data, ret){
                if(ret.url)
                {
                  window.open(ret.url);
                }
                Toastr.success("生成完成，正在下载中......");
              });
              Form.api.bindevent($("form[role=send]"),function (data,ret) {

              },function (data,ret) {
                if(ret.data)
                {
                  var htmlstr = '';
                  ret.data.Register.forEach(function (element, index, array) {
                    htmlstr +='<li>已被用户激活：'+array[index]+' </li>';
                  });
                  ret.data.codeNull.forEach(function (element, index, array) {
                    htmlstr +='<li>不存在：'+array[index]+' </li>';
                  });
                  ret.data.isCancel.forEach(function (element, index, array) {
                    htmlstr +='<li>已作废：'+array[index]+' </li>';
                  });
                  ret.data.send.forEach(function (element, index, array) {
                    htmlstr +='<li>已发货：'+array[index]+' </li>';
                  });
                  layer.open({
                    type: 1,
                    title: '数据有问题',
                    skin: 'layui-layer-demo', //样式类名
                    closeBtn: 1, //不显示关闭按钮
                    anim: 2,
                    shadeClose: true, //开启遮罩关闭
                    content: '<ul style="width: 222px">'+htmlstr+'</ul>'
                  });
                }

              });
            },
          formatter: {//渲染的方法
            sends: function (value, row, index) {
              var color = 'success';
              var text = '';
              if(row.send == 1)
              {
                color = 'success';
                text = '发货';
              }
              if(row.send==0)
              {
                color = 'yellow';
                text = '未发货';
              }
              return '<span class="text-'+color+'"><i class="fa fa-circle"></i>'+text+'</span>';
            },
            typeText:function(value, row, index){
              return Table.api.formatter.flag.call(row.type_text, value, row, index);
            },
              branch: function (value, row, index) {
                  return '<span class="text-default">'+row.branch_text+'</span>';
              },
              dealer: function (value, row, index) {
                  return '<span class="text-default">'+row.dealer_text+'</span>';
                },
            cancel: function (value, row, index) {
              var color = 'success';
              var text = '';
              if(row.is_cancel == 1)
              {
                color = 'success';
                text = '是';
              }
              if(!row.is_cancel)
              {
                color = 'danger';
                text = '否';
              }
              return '<span class="text-'+color+'"><i class="fa fa-circle"></i>'+text+'</span>';
            },
            register: function (value, row, index) {
              var color = 'success';
              var text = '';
              if(row.is_register == 1)
              {
                color = 'yellow';
                text = '已注册';
              }
              if(!row.is_register)
              {
                color = 'danger';
                text = '未注册';
              }
              return '<span class="text-'+color+'"><i class="fa fa-circle"></i>'+text+'</span>';
            }
          }
        }
    };
    return Controller;
});